package com.inspur.edp.udt.designtime.api.converter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inspur.edp.udt.designtime.api.entity.ComplexDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import com.inspur.edp.udt.designtime.api.exception.UdtModelErrorCodeEnum;
import com.inspur.edp.udt.designtime.api.exception.UdtModelException;
import com.inspur.edp.udt.designtime.api.utils.Context;
import com.inspur.edp.udt.designtime.api.utils.UdtThreadLocal;

import java.io.IOException;

public class UnifiedDataTypeDefConverter {
    String serializeResult = "{\n" +
            "    \"Type\": \"SimpleDataType\",\n" +
            "    \"Content\": {}}";
    String simpleUdtType = "SimpleDataType";
    String complexUdtType = "ComplexDataType";
    String para_Type = "Type";
    String para_Content = "Content";


    public String convertToDatabaseColumn(UnifiedDataTypeDef unifiedDataTypeDef) {
        Context context = new Context();
        context.setfull(false);
        UdtThreadLocal.set(context);
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode result = mapper.readTree(serializeResult);
            if (unifiedDataTypeDef.getClass().isAssignableFrom(SimpleDataTypeDef.class)) {
                String jsonResult = mapper.writeValueAsString((SimpleDataTypeDef) unifiedDataTypeDef);
                ((ObjectNode) result).put(para_Type, simpleUdtType);
                JsonNode sUdtJson = mapper.readTree(jsonResult);
                ((ObjectNode)result).set(this.para_Content, sUdtJson);
                return result.toString();
            } else if (unifiedDataTypeDef.getClass().isAssignableFrom(ComplexDataTypeDef.class)) {
                String jsonResult = mapper.writeValueAsString((ComplexDataTypeDef) unifiedDataTypeDef);
                ((ObjectNode) result).put(para_Type, complexUdtType);
                JsonNode cUdtJson = mapper.readTree(jsonResult);
                ((ObjectNode)result).set(this.para_Content, cUdtJson);
                return result.toString();
            } else {
                throw UdtModelException.createException(UdtModelErrorCodeEnum.GSP_BEMODEL_ENUM_NOTSUPPORT_0005, unifiedDataTypeDef.getClass().getTypeName());
            }
        } catch (IOException e) {
            throw UdtModelException.createException(UdtModelErrorCodeEnum.GSP_BEMODEL_JSON_0002, e, "UnifiedDataTypeDef", unifiedDataTypeDef.getName());
        }
//        ObjectMapper mapper = new ObjectMapper();
//        if (unifiedDataTypeDef.getClass().isAssignableFrom(SimpleDataTypeDef.class)) {
//            try {
//                return mapper.writeValueAsString((SimpleDataTypeDef)unifiedDataTypeDef);
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException("序列化单值UDT失败", e);
//            }
//        }
//        else if(unifiedDataTypeDef.getClass().isAssignableFrom(ComplexDataTypeDef.class)){
//            try {
//                return mapper.writeValueAsString((ComplexDataTypeDef)unifiedDataTypeDef);
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException("序列化多值UDT失败", e);
//            }
//        }
//        throw new RuntimeException("未识别的udt类型");
    }

    public UnifiedDataTypeDef convertToEntityAttribute(String json) {
        ObjectMapper mapper = new ObjectMapper();
//        UpdateElementService elementService= UpdateElementService.getInstance();
        JsonNode jsonNode = null;
        try {
//            jsonNode = mapper.readTree(handleJsonString(metaJsonNode.toString()));
            jsonNode = mapper.readTree(json);
        } catch (IOException e) {
            throw UdtModelException.createException(UdtModelErrorCodeEnum.GSP_BEMODEL_JSON_0001, e, "UnifiedDataTypeDef");
        }
        String componentType = jsonNode.get(para_Type).textValue();
//        String dataType = handleJsonString(jsonNode.get(para_Content).toString());
        String dataType = jsonNode.get(para_Content).toString();
        if (complexUdtType.equals(componentType)) {
            try {
                ComplexDataTypeDef complexDataTypeDef=mapper.readValue(dataType,ComplexDataTypeDef.class);
                complexDataTypeDef.updateColumnsInfo();
//                elementService.handleComplexUdtChildAsso(complexDataTypeDef);
                return complexDataTypeDef;
            } catch (IOException e) {
                throw UdtModelException.createException(UdtModelErrorCodeEnum.GSP_BEMODEL_JSON_0001, e, "ComplexDataTypeDef");
            }
        }
        if (simpleUdtType.equals(componentType)) {
            try {
                SimpleDataTypeDef simpleDataTypeDef=mapper.readValue(dataType, SimpleDataTypeDef.class);
                simpleDataTypeDef.updateColumnsInfo();
//                elementService.handleSimpleUdtChildAsso(simpleDataTypeDef);

                return simpleDataTypeDef;
            } catch (IOException e) {
                throw UdtModelException.createException(UdtModelErrorCodeEnum.GSP_BEMODEL_JSON_0001, e, "SimpleDataTypeDef");
            }
        }
        return null;
    }
}
